package com.yuanmei.charts;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.text.Spanned;

import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.yuanmei.views.R;
import com.yuanmei.common.Utils;

import java.util.ArrayList;
import java.util.List;

public class OBVChartDataSet implements TaChartData {
    private static final String TAG = OBVChartDataSet.class.getName();
    private List<Entry> mObvList;
    private List<Entry> mMaObvList;
    private List<BarEntry> mZBarList;

    private List<String> mXAxisDates;
    private CombinedData mOBVCombineData;
    private Context mContext;
    private KLineChartDataSet mRawDataSet;


    public OBVChartDataSet(Context context, KLineChartDataSet rawDataSet) {
        mContext = context;
        mRawDataSet = rawDataSet;

        mZBarList = new ArrayList<>();

        mObvList = new ArrayList<>();
        mMaObvList = new ArrayList<>();
        mXAxisDates = new ArrayList<>();
        double maSum = 0.0f;
        float obvSum = 0.0f;
        final int period = 5;
        for (int i = 0; i < mRawDataSet.getXAxisSize(); i++) {
            mXAxisDates.add(mRawDataSet.getXAxisLabel(i));
            float volume = mRawDataSet.getVolume(i);
            float va = volume;
            if (i > 0){
                float high = mRawDataSet.getHigh(i);
                float low = mRawDataSet.getLow(i);
                float close = mRawDataSet.getClose(i);
                float pOpen = mRawDataSet.getOpen(i - 1);
                float pClose = mRawDataSet.getClose(i - 1);

                // 改进算法
                if (close > Math.max(pOpen, pClose)) {
                    va = volume;
                } else if (close < Math.min(pOpen, pClose)) {
                    va = 0 - volume;
                } else if (high == low || close == pClose){
                    va = 0;
                } else {
                    va = ((close -low) - (high - close)/(high - low)) * volume;
                }
                // 原始算法
//                if (close > pClose) {
//                    va = volume;
//                } else if (close < pClose) {
//                    va = 0 - volume;
//                } else {
//                    va = 0;
//                }
            }
            obvSum += va;
            mObvList.add(new Entry(i, obvSum));
            float ma_obv;
            if (i < period) {
                maSum = maSum + obvSum;
                ma_obv = (float) maSum/(i+1);
            } else {
                maSum = maSum + obvSum - mObvList.get(i - period).getY();
                ma_obv = (float) maSum / period;
            }
            mMaObvList.add(new Entry(i, ma_obv));
            mZBarList.add(new BarEntry(i, Math.min(ma_obv, obvSum)));
        }

        ArrayList<ILineDataSet> lineDataSet = new ArrayList<>();
        LineDataSet obvSet =  new LineDataSet(mObvList, "OBV");
        TaLib.setCommonMaStyle(obvSet);
        obvSet.setHighlightEnabled(true);
        obvSet.setDrawHorizontalHighlightIndicator(true);
        obvSet.setDrawVerticalHighlightIndicator(true);
        obvSet.setHighLightColor(mContext.getResources().getColor(R.color.border_color));
        obvSet.setColor(mContext.getResources().getColor(R.color.red));
        lineDataSet.add(obvSet);

        LineDataSet maObvSet =  new LineDataSet(mMaObvList, "MA_OBV");
        TaLib.setCommonMaStyle(maObvSet);
        maObvSet.setColor(mContext.getResources().getColor(R.color.yellow));
        lineDataSet.add(maObvSet);

        BarDataSet barSet = new BarDataSet(mZBarList, "");
        barSet.setDrawValues(false);
        barSet.setBarShadowColor(mContext.getResources().getColor(R.color.transparent));
        barSet.setColors(mContext.getResources().getColor(R.color.transparent));
        BarData barData = new BarData(barSet);
        LineData linesData = new LineData(lineDataSet);

        mOBVCombineData = new CombinedData();
        mOBVCombineData.setData(barData);
        mOBVCombineData.setData(linesData);
    }

    @Override
    public int getXAxisSize() {
        return mXAxisDates.size();
    }

    @Override
    public String getXAxisLabel(int index) {
        String date = "None";
        if (index >= 0 && index < mXAxisDates.size()) {
            date = String.valueOf(mXAxisDates.get(index));
        }
        return date;
    }

    @Override
    public Spanned getDescription(int index) {
        String description = "OBV-> OBV:";
        if (index >= 0 && index < mObvList.size()) {
            description += Utils.priceToDecimalString(mObvList.get(index).getY());
        }

        if (index >= 0 && index < mMaObvList.size()) {
            description += ", MA_OBV:" + Utils.priceToDecimalString(mMaObvList.get(index).getY());
        }
        return Html.fromHtml(description);
    }

    @Override
    public List<String> getXAxisLabelList() {
        return mXAxisDates;
    }

    @Override
    public CombinedData getTaChartData() {
        return mOBVCombineData;
    }
}
